package com.yiqi.zhiyuan.feature.order.model;

import com.luck.picture.lib.rxbus2.RxBus;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.entity.bo.PageData;
import com.yiqi.zhiyuan.entity.dto.ChangeOrderDTO;
import com.yiqi.zhiyuan.entity.vo.ChangeReasonVO;
import com.yiqi.zhiyuan.entity.vo.FileVO;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.order.contract.ChangeContract;
import com.yiqi.zhiyuan.feature.order.contract.OrderContract;
import com.yiqi.zhiyuan.feature.order.dependencies.change.DaggerChangeModelComponent;
import com.yiqi.zhiyuan.feature.order.dependencies.info.DaggerOrderInfoModelComponent;
import com.yiqi.zhiyuan.feature.order.dependencies.order.DaggerOrderModelComponent;
import com.yiqi.zhiyuan.framework.api.ApiLib;
import com.yiqi.zhiyuan.framework.api.OrderApi;
import com.yiqi.zhiyuan.framework.api.UserApi;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @ProjectName: ZhiYuan
 * @Description: 注册model
 * @Author: lijuwen
 * @CreateDate: 2019/3/13 13:07
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/13 13:07
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ChangeModel implements ChangeContract.Model {
    @Inject
    OrderApi api;
    @Inject
    UserApi userApi;
    public ChangeModel() {
        DaggerChangeModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }
    @Override
    public Observable<List<ChangeReasonVO>> doSelectChangeReason() {
        return api.getOrderReason().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<FileVO> doUploadImage(String imageUrl) {
        File file = new File(imageUrl);
        RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        return userApi.uploadFile(filePart).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> doChangeOrder(ChangeOrderDTO orderDTO) {
        return api.reassignOrder(orderDTO).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}

